﻿#include "ConfigManager.h"
#include "CServer.h"
#include "AsioIOServicePool.h"

#include <csignal>
#include <boost/asio.hpp>
#include <iostream>

int main()
{
	try {
		auto& cfg = ConfigManager::getInstance();
		auto pool = AsioIOServicePool::getInstance();
		boost::asio::io_context ioc;
		boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
		signals.async_wait([&ioc, pool](auto, auto) {
			// io_context一旦停止，那么所有处理的异步事件马上返回
			ioc.stop();
			pool->stop();
		});

		auto port_str = cfg["SelfServer"]["Port"];
		CServer server( ioc, atoi(port_str.c_str()) );
		// 在CServer里绑定了监听对方连接的事件，ioc不会立刻退出
		ioc.run();
	}
	catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << std::endl;
	}
}

